﻿using log4net;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

public class Logger
{
    //定义日志对象
    private static readonly log4net.ILog tracing_Info = null;
    private static readonly log4net.ILog tracing_Error = null;
    private static readonly log4net.ILog tracing_Warn = null;
    private static readonly log4net.ILog tracing_Service = null;
    private static string filePath;
    /// <summary>
    /// 静态构造方法，初始参数
    /// </summary>
    static Logger()
    {
        tracing_Info = log4net.LogManager.GetLogger("loginfo");
        tracing_Error = log4net.LogManager.GetLogger("logerror");
        tracing_Warn = log4net.LogManager.GetLogger("logwarn");
        tracing_Service = log4net.LogManager.GetLogger("logservice");
    }

    /// <summary>
    /// 日志初始化,需要将log4net.config拷贝至gc.utility同目录,
    /// 如果没有则会自己创建一个默认的log4net.config
    /// </summary>
    public static void Init()
    {
        var ass = Assembly.GetExecutingAssembly();
        filePath = Path.Combine(Path.GetDirectoryName(ass.Location)) + @"\log4net.config";
        if (!File.Exists(filePath))
        {

            using (Stream s = ass.GetManifestResourceStream(ass.GetName().Name + ".log4net.config"))
            {
                using (StreamReader sr = new StreamReader(s))
                {
                    var str = sr.ReadToEnd();
                    File.WriteAllText(filePath, str);
                }
            }
        }
        log4net.Config.XmlConfigurator.Configure(new FileInfo(filePath));
    }
    public static void Init(string configPath)
    {
        log4net.Config.XmlConfigurator.Configure(new FileInfo(configPath));
    }


    /// <summary>
    /// 信息类日志写入方法
    /// </summary>
    /// <param name="message"></param>
    public static void WriteInfo(object message)
    {
        if (tracing_Info != null && tracing_Info.IsInfoEnabled)
        {
            tracing_Info.Info(message);
        }
    }

    public static void WriteInfo(object message, Exception ex)
    {
        if (tracing_Info != null && tracing_Info.IsInfoEnabled)
        {
            tracing_Info.Info(message, ex);
        }
    }

    /// <summary>
    /// 告警类日志写入方法（对字符串类型参数做了换行符和回车符替换处理）
    /// </summary>
    /// <param name="message"></param>
    public static void WriteWarn(object message)
    {
        if (tracing_Warn != null && tracing_Warn.IsWarnEnabled)
        {
            tracing_Warn.Warn(message);
        }
    }

    public static void WriteWarn(object message, Exception ex)
    {
        if (tracing_Warn != null && tracing_Warn.IsWarnEnabled)
        {
            tracing_Warn.Warn(message, ex);
        }
    }

    /// <summary>
    /// 错误类日志写入方法（对字符串类型参数做了换行符和回车符替换处理）
    /// </summary>
    /// <param name="message"></param>
    public static void WriteError(object message)
    {
        if (tracing_Error != null && tracing_Error.IsErrorEnabled)
        {
            tracing_Error.Error(message);
        }
    }

    public static void WriteError(object message, Exception ex)
    {
        if (tracing_Error != null && tracing_Error.IsErrorEnabled)
        {
            tracing_Error.Error(message, ex);
        }
    }

    /// <summary>
    /// 服务类消息
    /// </summary>
    /// <param name="message"></param>
    public static void WriteService(object message)
    {
        if (tracing_Service != null && tracing_Service.IsInfoEnabled)
        {
            tracing_Service.Info(message);
        }
    }

    public static void WriteService(object message, Exception ex)
    {
        if (tracing_Service != null && tracing_Service.IsInfoEnabled)
        {
            tracing_Service.Info(message, ex);
        }
    }
}

